﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;

namespace SenseUtil
{
    class Settings
    {
        const string CSTR_RegistryKey_HKCUTodayLeftSoftkey = @"Software\Microsoft\Today\Keys\112";
        const string CSTR_RegistryKey_HKCUTodayRightSoftkey = @"Software\Microsoft\Today\Keys\113";
        const string CSTR_TodaySoftKeyTxt = "";
        const string CSTR_TodaySoftKeyUrl = "Open";
        const string CSTR_TodaySoftKeyLeftTxt_Default = "Action";
        const string CSTR_TodaySoftKeyLeftUrl_Default = @"\windows\htcactionscreen.exe";
        const string CSTR_TodaySoftKeyRightTxt_Default = "Contacts"; 
        const string CSTR_TodaySoftKeyRightUrl_Default = @":MSCONTACTS";

        public static void SetSoftKey(int selkey, string text, string url, bool confirm)
        {
            bool do_action = true;
            //Check if we need to confirm first
            if (confirm)
            {
                if (MessageBox.Show("Are you sure?", "", MessageBoxButtons.OKCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button2) == DialogResult.Cancel)
                {
                    do_action = false;
                }
                else
                {
                    do_action = true;
                }
            }

            //Only do it if confirmed or no confirm required
            if (do_action)
            {
                RegistryKey keyLeft = Registry.CurrentUser.OpenSubKey(CSTR_RegistryKey_HKCUTodayLeftSoftkey, true);
                RegistryKey keyRight = Registry.CurrentUser.OpenSubKey(CSTR_RegistryKey_HKCUTodayRightSoftkey, true);

                //Create key if not present
                if (keyLeft == null)
                {
                    keyLeft = Registry.CurrentUser.CreateSubKey(CSTR_RegistryKey_HKCUTodayLeftSoftkey);
                }

                
                //Set the value depending on the keyselected
                //& Check to see if defaults are required
                switch (selkey)
                {
                    case 1://leftkey
                        keyLeft.SetValue(CSTR_TodaySoftKeyTxt, text);
                        keyLeft.SetValue(CSTR_TodaySoftKeyUrl, url);
                        break;

                    case 2://rightkey
                        if (keyRight == null)
                        {
                            keyRight = Registry.CurrentUser.CreateSubKey(CSTR_RegistryKey_HKCUTodayRightSoftkey);
                            keyRight = Registry.CurrentUser.OpenSubKey(CSTR_RegistryKey_HKCUTodayRightSoftkey, true);
                        }
                        keyRight.SetValue(CSTR_TodaySoftKeyTxt, text);
                        keyRight.SetValue(CSTR_TodaySoftKeyUrl, url);
                        break;

                    default:
                        try
                        {
                            //Key usually can-not be deleted it seems
                            keyRight.Close();
                            Registry.CurrentUser.DeleteSubKeyTree(CSTR_RegistryKey_HKCUTodayRightSoftkey);
                        }
                        catch
                        {
                            keyRight = Registry.CurrentUser.OpenSubKey(CSTR_RegistryKey_HKCUTodayRightSoftkey, true);
                            keyRight.SetValue(CSTR_TodaySoftKeyTxt, CSTR_TodaySoftKeyRightTxt_Default);
                            keyRight.SetValue(CSTR_TodaySoftKeyUrl, CSTR_TodaySoftKeyRightUrl_Default);
                        }
                        keyLeft.SetValue(CSTR_TodaySoftKeyTxt, CSTR_TodaySoftKeyLeftTxt_Default);
                        keyLeft.SetValue(CSTR_TodaySoftKeyUrl, CSTR_TodaySoftKeyLeftUrl_Default);
                        break;
                }
                keyRight.Close();
                keyLeft.Close();
            }
        }

        public static string GetApplicationFullPath()
        {
            //get the current path of the exe
            System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
            string textCurrentUtilExe = a.ManifestModule.FullyQualifiedName;

            return textCurrentUtilExe;
        }

        public static string GetApplicationPathOnly()
        {
            //get the current path of the exe
            System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
            string textCurrentUtilPath = a.ManifestModule.FullyQualifiedName;
            textCurrentUtilPath = textCurrentUtilPath.Replace(a.ManifestModule.Name, "");

            return textCurrentUtilPath;
        }



    }
}
